Skip to content

fix: Add PipelineVariable support to ModelTrainer fields (fixes #5524)#5608

Merged
mollyheamazon merged 2 commits intoaws:masterfrom
amitmodi:fix/pipeline-variable-model-trainer
Mar 9, 2026
Merged

fix: Add PipelineVariable support to ModelTrainer fields (fixes #5524)#5608
mollyheamazon merged 2 commits intoaws:masterfrom
amitmodi:fix/pipeline-variable-model-trainer

Conversation

@amitmodi
Copy link
Contributor

@amitmodi amitmodi commented Mar 7, 2026

Summary

This PR extends PipelineVariable support to ModelTrainer direct fields, unblocking V2-V3 migration for SageMaker Pipelines users.

Fixes: #5524

Problem

ModelTrainer uses Pydantic BaseModel with strict validation. Fields like training_image are typed as Optional[str], causing Pydantic to reject PipelineVariable objects (e.g. ParameterString) with a ValidationError. This blocks any customer using SageMaker Pipelines from migrating to V3.

Solution

Follow the existing V3 pattern already established by SourceCode, OutputDataConfig, and Compute - use the StrPipeVar type alias (Union[str, PipelineVariable]) which already exists in pipeline_variable.py with Pydantic compatibility via get_pydantic_core_schema().

Changes (1 file, 5 insertions, 4 deletions)

  • training_image: Optional[str] -> Optional[StrPipeVar]
  • algorithm_name: Optional[str] -> Optional[StrPipeVar]
  • training_input_mode: Optional[str] -> Optional[StrPipeVar]
  • environment: Dict[str, str] -> Dict[str, StrPipeVar]

Testing

Before: ModelTrainer(training_image=ParameterString(name='img')) raises ValidationError
After: ModelTrainer(training_image=ParameterString(name='img')) works

Note

This is step 1. Integer/boolean fields in nested configs (Compute, Networking) need IntPipeVar/BoolPipeVar in a follow-up PR.

)

Extend StrPipeVar type to ModelTrainer's direct fields:
- training_image: Optional[str] -> Optional[StrPipeVar]
- algorithm_name: Optional[str] -> Optional[StrPipeVar]
- training_input_mode: Optional[str] -> Optional[StrPipeVar]
- environment: Dict[str, str] -> Dict[str, StrPipeVar]

This follows the existing V3 pattern already used by SourceCode,
OutputDataConfig, and Compute (for instance_type). The StrPipeVar
type alias and PipelineVariable.__get_pydantic_core_schema__()
already exist in the codebase.

This unblocks V2->V3 migration for SageMaker Pipelines users who
need to pass ParameterString to ModelTrainer fields.

Fixes aws#5524
…ble-safe logging

- Add test_model_trainer_pipeline_variable.py with 9 tests:
  - 4 PipelineVariable acceptance tests (training_image, algorithm_name,
    training_input_mode, environment)
  - 4 regression tests (real string values still work)
  - 1 invalid type rejection test
- Fix PipelineVariable-safe logging in model_post_init (avoid __str__
  on PipelineVariable which raises TypeError)

All 57 tests pass (48 existing + 9 new, 0 regressions).
@amitmodi amitmodi temporarily deployed to manual-approval March 7, 2026 18:39 — with GitHub Actions Inactive
@amitmodi
Copy link
Contributor Author

amitmodi commented Mar 9, 2026

The 3 codestyle-doc-tests failures (sagemaker-train, sagemaker-mlops, sagemaker-serve) are pre-existing on master — confirmed identical failures on unrelated PR #5602. This PR only modifies model_trainer.py and its test file. All unit-tests, integ-tests, codecov, CodeQL, and readthedocs checks pass. Could a maintainer please merge with admin override?

@mollyheamazon mollyheamazon merged commit 0906526 into aws:master Mar 9, 2026
14 of 17 checks passed
@admivsn
Copy link
Contributor

admivsn commented Mar 10, 2026

Thanks for this @amitmodi @mollyheamazon. I could help contribute with the follow up PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants